package volatiledemo;
class StoppableTask extends Thread {

    //boolean flag = false;
    volatile boolean flag = false;
    int i = 0;

    public void run() {
        while (!flag) {
            i++;
        }
    }
}
public class VolatileDemo {

    public static void main(String[] args) throws Exception {
        StoppableTask st = new StoppableTask();
        st.start();
        Thread.sleep(2000);
        st.flag = true;
        System.out.println("stopped, i=" + st.i);
    }
}
